<?php

namespace App\Http\Controllers\App;

use App\Http\Middleware\Member;
use App\Models\BonusPool;
use App\Models\BonusPoolLog;
use App\Models\MemberBonusQueue;
use App\Models\MemberOrder;
use App\Models\MemberOrderCount;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BonusPoolController extends Controller
{
    // todo 获取奖金池金额， 当前排队人数， 我的排名

    public function index()
    {
        $bonus_pool = BonusPool::get();
        $member_bonus_num = MemberBonusQueue::where("status", 0)->count();
        $member_bonus_prize_num = MemberBonusQueue::where("status",1)->count();
        $sub =\DB::table(\DB::raw("member_bonus_queue a,(select @rownum :=0) b"))
            ->select(\DB::raw("a.id, a.member_id,(@rownum:=@rownum+1) as rownum"))
            ->where("a.status", 0)->orderBy("created_at", "asc");
        $rowno = \DB::table(\DB::raw("({$sub->toSql()}) as c"))
            ->mergeBindings($sub)
            ->where('c.member_id', \Auth::id() ? \Auth::id() :6)
            ->value('c.rownum');

        $turnover =   \DB::select('SELECT DATE_FORMAT(pay_time,"%Y-%m") as pay_times, sum(money) as money FROM member_order GROUP BY pay_times');
        // 订单统计
        $member_order_count = MemberOrderCount::get();
        // 我的获奖次数
        $member_prize_num = MemberBonusQueue::whereMemberId(\Auth::id())->where("status",1)->count();
       return  $this->renderJson([
            "total_money"  => $bonus_pool[0]->total_money, // 奖金池-历史总金额
            "usable_money" =>  $bonus_pool[0]->usable_money, // 奖金池-可用总金额
            "member_queue_num" => $member_bonus_num, // 当前排队人数
            "member_bonus_prize_num"=>$member_bonus_prize_num,  // 获取奖人数
            "row_no" => $rowno, // 我的排名
            "member_order_count_num"=>$member_order_count[0]->num, //  在线消费人数
            "member_prize_num" => $member_prize_num, // 我的获奖次数
            "member_order_count_money" => $member_order_count[0]->money,  //总营业额
            "turnover" => $turnover  //图表数据
        ]);
    }

    // todo 历史营业额列表
    public function historyTurnover(Request $request)
    {
        $time = $request->input("type");
        $where = "";
        switch ($time){
            case "today":
                $where = "to_days(t1.pay_time) = To_days(now())";
                break;
            case "yesterday":
                $where = "(To_days(now()) - To_days(t1.pay_time)) <= 1";
                break;
            case "seven_days_ago":
                $where = "DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(t1.pay_time)";
                break;
            case "this_month":
                $where = "date_format(t1.pay_time,'%Y-%m') = date_format(curdate(), '%Y-%m')";
                break;
        }

        $member_order = \DB::table("member_order as t1")
            ->leftJoin("store as t2","t1.store_id","=","t2.id")
            ->where("t1.is_pay", 1)
            ->whereRaw($where)
            ->select("t1.money", "t1.pay_time", "t2.id","t2.name", "t2.province_name", "t2.city_name", "t2.area", "t2.logo")
            ->paginate();
        return $this->renderJson($member_order);
    }

    // todo 获取奖金池流入日志
    public function income()
    {
        $bonus_pool_log = \DB::table("bonus_pool_log as t1")
            ->leftJoin("store as t2", "t1.store_id","=","t2.id")
            ->select("t1.money","t1.created_at", "t2.id","t2.name", "t2.province_name", "t2.city_name", "t2.area", "t2.logo")
            ->paginate();
        return $this->renderJson($bonus_pool_log);
    }

    // todo 获取奖金池流出 资金体现日志

    public function reflect()
    {
        $data = \DB::table("member_income_log as t1")
            ->leftJoin("members as t2","t1.member_id","=","t2.id")
            ->selectRaw("t1.money, t1.created_at, concat_ws('****',
            substring(t2.phone, 1, 3),
            substring(t2.phone, 8, 4)) AS phone")
            ->paginate();
        return $this->renderJson($data);
    }

    // todo  获取当前排队人数
    public function queueMember()
    {
        $data = \DB::table("member_bonus_queue as t1")
            ->leftJoin("members as t2", "t1.member_id", "=", "t2.id")
            ->where("t1.status","=",0)
            ->selectRaw("t1.created_at, t2.avatar, concat_ws('****',
            substring(t2.phone, 1, 3),
            substring(t2.phone, 8, 4)) AS phone")
            ->orderBy("t1.created_at","asc")
            ->paginate();
        return $this->renderJson($data);
    }

    // todo 已经获奖人数列表
    public function getMemberBonusQueue()
    {
        $data = \DB::table("member_bonus_queue as t1")
            ->leftJoin("members as t2", "t1.member_id", "=", "t2.id")
            ->where("t1.status","=",1)
            ->selectRaw("t1.created_at, t2.avatar, concat_ws('****',
            substring(t2.phone, 1, 3),
            substring(t2.phone, 8, 4)) AS phone")
            ->orderBy("t1.created_at","asc")
            ->paginate();
        return $this->renderJson($data);
    }

    // todo 在线消费人数

    public function memberOrder(Request $request)
    {
        $time = $request->input("type");
        $where = "";
        switch ($time){
            case "today":
                $where = "to_days(t1.pay_time) = To_days(now())";
                break;
            case "yesterday":
                $where = "(To_days(now()) - To_days(t1.pay_time)) <= 1";
                break;
            case "seven_days_ago":
                $where = "DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(t1.pay_time)";
                break;
            case "this_month":
                $where = "date_format(t1.pay_time,'%Y-%m') = date_format(curdate(), '%Y-%m')";
                break;
        }

        $data = \DB::table("member_order as t1")
            ->leftJoin("members as t2","t1.member_id","=","t2.id")
            ->where("t1.is_pay", 1)
            ->whereRaw($where)
            ->selectRaw("t1.money, t1.created_at, t2.avatar, concat_ws('****',
            substring(t2.phone, 1, 3),
            substring(t2.phone, 8, 4)) AS phone")
            ->orderBy("t1.created_at","asc")
            ->paginate();
        return $this->renderJson($data);
    }




}
